package fhp.hlhj.giantfold.customView

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Environment
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.FileCallback
import com.lzy.okgo.convert.FileConvert
import com.lzy.okgo.model.Progress
import com.lzy.okgo.model.Response
import com.lzy.okrx.adapter.ObservableBody
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.network.Urls
import fhp.hlhj.giantfold.utils.LogUtil
import java.io.File
import android.support.v4.content.ContextCompat.startActivity
import android.content.Intent.ACTION_VIEW
import android.net.Uri
import android.os.Build
import kotlinx.android.synthetic.main.download_dialog.*
import android.support.v4.content.ContextCompat.startActivity
import android.support.v4.content.FileProvider




/**
 * Created by Administrator on 2018\2\23 0023.
 */
class DownloadDialog(context: Context,private val downLoadUrl:String,private val activity: Activity) :Dialog(context, R.style.CustomDialog){
    var PHOTO_DIR = File(Environment.getExternalStorageDirectory().toString() + "/juzheapk")//设置保存路径
    var avaterFile = File(PHOTO_DIR, "juzhe.apk")//设置文件名称
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.download_dialog)
        if (!PHOTO_DIR.exists()){
            PHOTO_DIR.mkdir()
        }
        if (avaterFile.exists()) {
            avaterFile.delete()
        }
        avaterFile.createNewFile()
        this.setCancelable(false)
        OkGo.get<File>(Urls.BaseUrl+downLoadUrl)
                .execute(object :FileCallback(PHOTO_DIR.toString(),"juzhe.apk"){
                    override fun onSuccess(p0: Response<File>?) {
                        val intent = Intent(Intent.ACTION_VIEW)
                        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                        //Android 7.0 系统共享文件需要通过 FileProvider 添加临时权限，否则系统会抛出 FileUriExposedException .
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                            val contentUri = FileProvider.getUriForFile(context, "fhp.hlhj.giantfold.fileprovider", p0?.body())
                            intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
                        } else {
                            intent.setDataAndType(
                                    Uri.fromFile(p0?.body()),
                                    "application/vnd.android.package-archive")
                        }
                        activity.startActivity(intent)
                        this@DownloadDialog.dismiss()
                    }
                    override fun downloadProgress(progress: Progress?) {
                        LogUtil.log(progress.toString())
                        downProgress.progress= (progress?.fraction!! *100).toInt()
                        downPercent.text=(progress?.fraction!! *100).toInt().toString()+"%"
                    }
                })
    }


}